home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / comm / mail / YAM23src.lha / Source / gen_include.pl < prev    next >
Perl Script  |  2000-11-18  |  6KB  |  228 lines

  1. #! /bin/perl
  2. #
  3. # Extends include-header-files with __attribute__((packed))
  4. # to make ixemul and os-includes ppc-compatible
  5. #
  6. # 07.06.98 Samuel Devulder: __attribute((aligned(2)) for all >2 byte elements
  7. # 18.06.98 Holger Jakob:    It is not enough to use a modified
  8. #                           exec/types.h only :(
  9. # 21.05.2000 Emmanuel Lesueur: don't put __attribute__((aligned(2)) for structure
  10. #                              elements that are on correct boudaries.
  11. #
  12. require 5.002;
  13.  
  14. # structs that have a length non multiple of 4:
  15.  
  16. %oddstructs=(
  17.     AChain => 1,
  18.     AnchorPath => 1,
  19.     AnimComp => 1,
  20.     AnimOb => 1,
  21.     AppMessage => 1,
  22.     AvailFonts => 1,
  23.     AvailFontsHeader => 1,
  24.     CDInfo => 1,
  25.     CIA => 1,
  26.     ClipboardUnitPartial => 1,
  27.     ClockData => 1,
  28.     ColorRegister => 1,
  29.     CopIns => 1,
  30.     CopList => 1,
  31.     Custom => 1,
  32.     DTSpecialInfo => 1,
  33.     DataType => 1,
  34.     DateTime => 1,
  35.     Device => 1,
  36.     DeviceTData => 1,
  37.     DiscResourceUnit => 1,
  38.     DiskFontHeader => 1,
  39.     DiskObject => 1,
  40.     DosInfo => 1,
  41.     DosLibrary => 1,
  42.     DrawInfo => 1,
  43.     DrawerData => 1,
  44.     FileSysEntry => 1,
  45.     GadgetInfo => 1,
  46.     GlyphWidthEntry => 1,
  47.     IEPointerTablet => 1,
  48.     IODRPReq => 1,
  49.     IOExtPar => 1,
  50.     IOExtSer => 1,
  51.     IOPrtCmdReq => 1,
  52.     InputEvent => 1,
  53.     Interrupt => 1,
  54.     Isrvstr => 1,
  55.     KeyMapNode => 1,
  56.     Layer_Info => 1,
  57.     Library => 1,
  58.     List => 1,
  59.     Menu => 1,
  60.     Node => 1,
  61.     NotifyMessage => 1,
  62.     PrefHeader => 1,
  63.     PrinterData => 1,
  64.     PrinterExtendedData => 1,
  65.     PrinterGfxPrefs => 1,
  66.     PrinterSegment => 1,
  67.     PrtInfo => 1,
  68.     PubScreenNode => 1,
  69.     RGBTable => 1,
  70.     Resident => 1,
  71.     RexxTask => 1,
  72.     SCSICmd => 1,
  73.     SatisfyMsg => 1,
  74.     SerialPrefs => 1,
  75.     SignalSemaphore => 1,
  76.     SpecialMonitor => 1,
  77.     TAvailFonts => 1,
  78.     TOCEntry => 1,
  79.     TOCSummary => 1,
  80.     ToolNode => 1,
  81.     Unit => 1,
  82.     View => 1,
  83.     ViewPortExtra => 1,
  84.     bltnode => 1,
  85.     cprlist => 1,
  86. );
  87.  
  88. undef $/;
  89.  
  90. die "no include" if not $include=@ARGV[0];
  91. print STDERR "Copying $include ... ";
  92.  
  93. open(INP,"<$include");
  94. $source=<INP>;
  95.  
  96. # Check for additional patches here...
  97. # user.h, (screens.h), setjmp.h...
  98.  
  99. #if( $include=~ /include\/user.h$/i ) { $source=~ s/(\W)jmp_buf(\W)/$1jmp_buf_m68k$2/g; }
  100. #if( $include=~ /include\/setjmp.h$/i ) {
  101. #  $source=~ s/(#define\s+_JBLEN)(\s+\d+)/$1_M68K $2\n$1\t\t26+17*2/;
  102. #  $source=~ s/(typedef\s+int\s+sigjmp_buf)(.*)(_JBLEN)(.*)/$1_m68k$2$3_M68K$4\n$1$2$3$4/;
  103. #  $source=~ s/(typedef\s+int\s+jmp_buf)(.*)(_JBLEN)(.*)/$1_m68k$2$3_M68K$4\n$1$2$3$4/;
  104. #}
  105. if( $include=~ /include\/sys\/syscall.h$/i ) { $source=~ s/#ifndef\s+?_KERNEL(.*?)#endif//s; }
  106.  
  107. #
  108. #
  109.  
  110. $source=~ s/\/\*.*?\*\///sg;  # Sorry, no comments
  111. $source=~ s/^(\s*)struct((.|\n)*?)({(.|\n)*?});/&ins_packed_struct($2,$4)/meg;
  112. # ToDo: same for typdef
  113. #$source=~ s/^typedef((.|\n)*?)((\w|\n)*?);/&ins_packed_typedef($1,$3)/meg;
  114. print $source;
  115.  
  116. close(INP);
  117. print STDERR "Applied ";
  118. print STDERR ($source=~ s/__attribute/__attribute/g) || "no";
  119. print STDERR " patches.\n";
  120.  
  121. $alignment=0;
  122. $max_align=0;
  123.  
  124. sub ins_packed_struct
  125. {
  126.     local ($name,$text)=@_;
  127.     local ($return);
  128.  
  129.     $alignment=0;
  130.     $max_align=0;
  131.  
  132. #       $text=~ s/(LONG|struct)([^;])/$1$2 __attribute__((aligned(2))) /g;
  133.  
  134.     $return="struct".$name.$text." __attribute__((packed));";
  135.  
  136. #FIXME: /* ; */ is not recogniced and 2-word types(eg. unsigned int) as well
  137. #FIXED!?
  138.     $return=~ s/^(\s*)(\w*)(\s*)([a-zA-Z0-9_]*)(.*?);/&ins_align($1,$2,$3,$4,$5)/ge;
  139.  
  140.     if($max_align>1 && $alignment>0 && ($alignment&1)!=0) {
  141.         $return=~ s/(.*)}(\s*__attribute__\(\(packed\)\).*)/$1\tchar __pad__;\n}$2/;
  142.     }
  143.     return $return;
  144. }
  145. sub ins_packed_typedef
  146. {
  147.     local ($text,$name)=@_;
  148.     local ($return);
  149.  
  150.  
  151. #       $text=~ s/(LONG|struct)([^;])/$1$2 __attribute__((aligned(2))) /g;
  152.  
  153. #       $return="struct".$name.$text." __attribute__((packed));";
  154.  
  155. #FIXME: /* ; */ is not recogniced and 2-word types(eg. unsigned int) as well
  156. #FIXED!?
  157. #       $return=~ s/^(\s*)(\w*)(\s*)([a-zA-Z_]*)(.*?);/&ins_align($1,$2,$3,$4,$5)/ge;
  158.  
  159.     return "typedef $name;";
  160. }
  161.  
  162. sub ins_align
  163. {
  164.     local ($space,$type,$space2,$type2,$part1)=@_;
  165.     local ($size,$x);
  166.     $size=0;
  167.  
  168.     if ( $part1=~ /^\s*\*/ ) {
  169.         $size=4;
  170.         if ( $alignment!= 0 ) {
  171.         if ( $part1=~ /^\s*(\**)(\w*)(.*)/ ) {
  172.             $part1=$1." __attribute__((aligned(2))) ".$2.$3;
  173.         }
  174.         }
  175.     } elsif( $type=~ /^(BYTE|UBYTE|BYTEBITS|TEXT|char)$/ ) {
  176.         $size=1;
  177.     } elsif( $type=~ /^(WORD|UWORD|SHORT|USHORT|BOOL|COUNT|UCOUNT|WORDBITS|short)$/ ) {
  178.         $size=2;
  179.     } elsif( $type=~ /^struct$/ ) {
  180.         if ($alignment != 0) {
  181.         $type2=$type2." __attribute__((aligned(2)))";
  182.         }
  183.         if( exists $oddstructs{$type2} ) {
  184.         $size=2;
  185.         } else {
  186.         $size=4;
  187.         }
  188.     } elsif( $alignment!=0 ) {
  189.         if( $type=~ /^([AC]PTR|STRPTR|LONG|LONGBITS|ULONG|FLOAT|DOUBLE)$/ ) {
  190.         $type=$type." __attribute__((aligned(2)))";
  191.         $size=4;
  192.         } elsif( $type=~ /^unsigned$/ ) {
  193.         $type2=$type2." __attribute__((aligned(2)))";
  194.         $size=4;
  195.         } elsif( $type=~ /^(int|long)$/ ) {
  196.         $type=$type." __attribute__((aligned(2)))";
  197.         $size=4;
  198.         } elsif( $part1=~ /^(\*|\(\*)/ ) {
  199.         $type=$type." __attribute__((aligned(2)))";
  200.         $size=4;
  201.         }
  202.     }
  203.     if( $alignment==1 && $size>1 ) {
  204.         $alignment=2;
  205.     } elsif( $alignment==3 && $size>1) {
  206.         $alignment=0;
  207.     }
  208.     if( $size!=0 && $alignment!=-1 ) {
  209.         $x=$part1;
  210.         while ($alignment!=-1 && $x=~/\[/) {
  211.         if( $x=~ /\[(\d*)\](.*)/ ) {
  212.             $size*=$1;
  213.             $x=$2;
  214.         } else {
  215.             $alignment=-1;
  216.         }
  217.         }
  218.         if( $alignment!=-1 ) {
  219.         $alignment=($alignment+$size)&3;
  220.         }
  221.     }
  222.     if( $size > $max_align) {
  223.         $max_align = $size;
  224.     }
  225. #        return "/* ".$alignment.",".$max_align." */ ".$space.$type.$space2.$type2.$part1.";";
  226.     return $space.$type.$space2.$type2.$part1.";";
  227. }
  228.